/*
加入房间界面
*/
var tipsConf = require("tips").tipsConf;

cc.Class({
    extends: cc.Component,

    properties: {
        parent:{
            default:null,
            type:cc.Node
        },
        isInit:false,
    },

    onLoad: function () {
        this.joinRoomID = "";
        this.roomNumNode = this.node.getChildByName("roomNumList");
        this.roomNumList = {};
        for(var i=1;i<=6;i++)
            this.roomNumList[i] = this.roomNumNode.getChildByName("num"+i).getChildByName("label").getComponent("cc.Label");
        this.curRoomIDCount = 0;
    },

    onInit:function(){
        this.joinRoomID = "";
        this.roomNumNode = this.node.getChildByName("roomNumList");
        this.roomNumList = {};
        for(var i=1;i<=6;i++)
            this.roomNumList[i] = this.roomNumNode.getChildByName("num"+i).getChildByName("label").getComponent("cc.Label");
        this.curRoomIDCount = 0;

        this.isInit = true;
    },

    onBtnClick:function(event, customEventData){
        var index = parseInt(customEventData);
        if(index >= 0 && index <= 9)
        {
            if(this.curRoomIDCount < 6)
            {
                this.joinRoomID += customEventData;
                this.curRoomIDCount ++;
                this.roomNumList[this.curRoomIDCount].string = index;
            }
            if(this.curRoomIDCount == 6)
            {
                var self = this;
                var roomId = parseInt(this.joinRoomID);
                pomelo.request("connector.entryHandler.sendData", {"code" : "join","params" : {"roomId":roomId}}, function(data) {
                    console.log("join join join!!!!!!!!!!");
                    console.log(data);
                    self.cleanRoomId();
                    if(data.msg.code && data.msg.code == 119)
                    {
                        pomelo.disconnect();
                        return;
                    }
                    if(!data.flag){
                        if(data.msg && data.msg.code)
                            window.clientGlobal.tipsBox.showLayer(1,tipsConf[data.msg.code]);
                        else
                            window.clientGlobal.tipsBox.showLayer(1,"加入房间失败,请重新创建!");
                    }
                });
            }
        }
        else if(index == 10){
            if(this.curRoomIDCount != 0)
            {
                this.joinRoomID = this.joinRoomID.substring(0,this.joinRoomID.length-1)
                this.roomNumList[this.curRoomIDCount].string = "";
                this.curRoomIDCount --;
            }
        }
        else if(index == 11){
            this.joinRoomID = "";
            this.curRoomIDCount = 0;
            for(var i=1;i<=6;i++)
                this.roomNumList[i].string = "";
        }else if(index == 12){
            console.log("测试粘贴按钮")
        }

    },

    cleanRoomId:function(){
        this.joinRoomID = "";
        this.curRoomIDCount = 0;
        for(var i=1;i<=6;i++)
           this.roomNumList[i].string = "";
    },

    showLayer:function(){
        if(this.isInit == false)
            this.onInit();
        this.node.active = true;
    },

    hideLayer:function(){
        this.node.active = false;
    },
});
